<?php

namespace app\common\service;

use app\admin\model\KnscyTickets;
use app\admin\model\KnscyTicketsPrice;
use think\facade\Db;

/**
 * 计算门票的价格日历
 */
class TicketPriceService
{
    /**
     * 默认获取day天到 month个月之间的每日价格
     * @param $day
     * @param $ticket_id
     * @param $month
     * @return array
     */
    public static function getMonthPriceList($day, $ticket_id, $month = 12, $format = true)
    {
        $month_first_day = $day;
        //获取一年后的当天
        $month_last_day = date('Y-m-d', strtotime("+{$month} months", strtotime($day)));


        $range = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 1)
            ->order(Db::raw('sort=0, sort desc,id asc'))
            //srot值小的，id大的会覆盖旧记录 sort 有排序功能
            //可以理解成 最后添加的记录覆盖历史价格记录
            ->select();
        //获取周的规则
        $weeks = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 2)
            ->field('price,id,content as weeks') //这里因为content属性被转换过，取别名可以跳过转换
            ->order(Db::raw('sort=0, sort desc,id asc'))
            ->select();
        $week_price = [];
        if (!empty($weeks)) {
            foreach ($weeks as $week) {
                $ws = explode(",", $week->weeks);
                $ws = array_map('trim', $ws);
                foreach ($ws as $w) {
                    $week_price[$w] = $week->price;
                }
            }
        }
        //获取日的规则
        $per_days = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 3)
            ->order(Db::raw('sort=0, sort asc,id asc'))
            ->select();
        $day_price = [];
        if (!empty($per_days)) {
            foreach ($per_days as $p) {
                $ws = explode(",", $p->content);
                $ws = array_map('trim', $ws);
                foreach ($ws as $w) {
                    $day_price[$w] = $p->price;
                }
            }
        }
        //开始计算价格
        $price = [];
        if (!empty($range)) {
            foreach ($range as $k => $v) {
                $days = explode(' - ', $v->content);
                $days = array_map('trim', $days);
                $next_day = $days[0];
                do {
                    $price[$next_day] = $v->price;
                    //继续查找下一天
                    $next_day = date('Y-m-d', strtotime('+1 day', strtotime($next_day)));


                } while (count($days) == 2 && strtotime($next_day) <= strtotime($days[1]));
            }
        }
        //最后得出当月的价格
        $month_price = [];
        $next_day = $month_first_day;
        do {
            //如果周有设置覆盖
            $w = date('w', strtotime($next_day));
            if (isset($week_price[$w])) {
                $price[$next_day] = $week_price[$w];
            }
            //如果当日有价格
            if (isset($day_price[$next_day])) {
                $price[$next_day] = $day_price[$next_day];
            }
            if (isset($price[$next_day])) {
                if ($format) {
                    $str = '%d<span class="mark_icon">¥%s</span>';
                    $month_price[$next_day] = sprintf($str, explode("-", $next_day)[2], $price[$next_day]);;

                } else {
                    $month_price[$next_day] = $price[$next_day];
                }
            }
            $next_day = date('Y-m-d', strtotime('+1 day', strtotime($next_day)));

        } while (strtotime($next_day) <= strtotime($month_last_day));

        return $month_price;
    }

    /**
     * 当日的门票价格
     * @param array $ticket_ids
     * @return array
     */
    public static function getTicketTodayPrice($ticket_ids)
    {
        $day = self::get_start_day();
        $price = [];
        foreach ($ticket_ids as $ticket_id) {
            $price[$ticket_id] = self::getdayPrice($day, $ticket_id);
        }
        return $price;
    }

    public  static function get_start_day()
    {
        $day = date('Y-m-d');
        if(strtotime($day. " 16:00:00") < time()){
            $day = date('Y-m-d', strtotime("+1 day"));
        }
        return $day;
    }

    /**
     * 获取门票指定日期的价格
     * @param $day
     * @param $ticket_id
     * @return int
     */
    public static function getdayPrice($day, $ticket_id)
    {
        $today = date('Y-m-d', strtotime($day));

        $range = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 1)
            ->order(Db::raw('sort=0, sort desc,id asc'))
            //srot值小的，id大的会覆盖旧记录 sort 有排序功能
            //可以理解成 最后添加的记录覆盖历史价格记录
            ->select();
        //获取周的规则
        $weeks = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 2)
            ->field('price,id,content as weeks') //这里因为content属性被转换过，取别名可以跳过转换
            ->order(Db::raw('sort=0, sort desc,id asc'))
            ->select();
        $week_price = [];
        if (!empty($weeks)) {
            foreach ($weeks as $week) {
                $ws = explode(",", $week->weeks);
                $ws = array_map('trim', $ws);
                foreach ($ws as $w) {
                    $week_price[$w] = $week->price;
                }
            }
        }
        //获取日的规则
        $per_days = KnscyTicketsPrice::where('ticket_id', $ticket_id)
            ->where('type_id', 3)
            ->order(Db::raw('sort=0, sort asc,id asc'))
            ->select();
        $day_price = [];
        if (!empty($per_days)) {
            foreach ($per_days as $p) {
                $ws = explode(",", $p->content);
                $ws = array_map('trim', $ws);
                foreach ($ws as $w) {
                    if ($w == $today)
                        $day_price[$w] = $p->price;
                }
            }
        }
        //开始计算价格
        $price = [];
        if (!empty($range)) {
            foreach ($range as $k => $v) {
                $days = explode(' - ', $v->content);
                $days = array_map('trim', $days);
                $next_day = $days[0];
                do {
                    if ($next_day == $today) {

                        $price[$next_day] = $v->price;
                    }
                    //继续查找下一天
                    $next_day = date('Y-m-d', strtotime('+1 day', strtotime($next_day)));


                } while (count($days) == 2 && strtotime($next_day) <= strtotime($days[1]));
            }
        }
        //最后得出当月的价格
        $month_price = [];
        $next_day = $today;

        //如果周有设置覆盖
        $w = date('w', strtotime($next_day));
        if (isset($week_price[$w])) {
            $price[$next_day] = $week_price[$w];
        }
        //如果当日有价格
        if (isset($day_price[$next_day])) {
            $price[$next_day] = $day_price[$next_day];
        }
        if (isset($price[$next_day])) {
            $month_price[$next_day] = $price[$next_day];
        }

        if (empty($month_price[$next_day])) {
            //如果没有设置动态价格，按门票的price
            $ticket = KnscyTickets::find($ticket_id);
            $ticket_price = 0;
            if (!empty($ticket)) {
                $ticket_price = $ticket->price;
            }
            return number_format($ticket_price);
        } else {

            return number_format($month_price[$next_day]);
        }

    }
}